package com.surface.hao.component;

import javax.swing.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.io.IOException;

/**
 * @Description:
 *  自定义一个右键有弹出框的TextArea
 * @Author: songhao
 * @Email:425008064@qq.com
 * @CreateDate: 2019/12/30 14:49
 */
public class JPopupTextArea extends JTextArea implements MouseListener, ActionListener {

    private JPopupMenu popupMenu = null;
    private JMenuItem cutMenu = null;//剪切
    private JMenuItem copyMenu = null;//复制
    private JMenuItem pasteMenu = null;//粘贴
    private JMenuItem selectAllMenu = null;//选中
    private JMenuItem clearMenu = null;//清空

    public JPopupTextArea() {
        super();
        popupMenu = new JPopupMenu();

        cutMenu = new JMenuItem("Cut");
        copyMenu = new JMenuItem("Copy");
        pasteMenu = new JMenuItem("Paste");
        selectAllMenu = new JMenuItem("Select All");
        clearMenu = new JMenuItem("Clear All");

        cutMenu.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
        copyMenu.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
        pasteMenu.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
        selectAllMenu.setAccelerator(KeyStroke.getKeyStroke('A', InputEvent.CTRL_MASK));
        clearMenu.setAccelerator(KeyStroke.getKeyStroke('D', InputEvent.CTRL_MASK));

        cutMenu.addActionListener(this);
        copyMenu.addActionListener(this);
        pasteMenu.addActionListener(this);
        selectAllMenu.addActionListener(this);
        clearMenu.addActionListener(this);

        popupMenu.add(cutMenu);
        popupMenu.add(copyMenu);
        popupMenu.add(pasteMenu);
        popupMenu.add(new JSeparator());
        popupMenu.add(selectAllMenu);
        popupMenu.add(clearMenu);

        this.add(popupMenu);
        this.addMouseListener(this);

    }
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == copyMenu){
            this.copy();
        }
        if(e.getSource() == pasteMenu){
            this.paste();
        }
        if(e.getSource() == cutMenu){
            this.cut();
        }
        if(e.getSource() == selectAllMenu){
            this.selectAll();
        }
        if(e.getSource() == clearMenu){
            this.clearContent();
        }

    }

    private void clearContent() {
        this.setText("");
    }

    public void mousePressed(MouseEvent e) {

        popupMenuTrigger(e);
    }

    public void mouseReleased(MouseEvent e) {

        popupMenuTrigger(e);
    }

    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    private void popupMenuTrigger(MouseEvent e){

        if(e.isPopupTrigger()){

            this.requestFocusInWindow();

            cutMenu.setEnabled(isAbleToCopyAndCut());
            copyMenu.setEnabled(isAbleToCopyAndCut());
            pasteMenu.setEnabled(isAbleToPaste());
            selectAllMenu.setEnabled(isAbleToSelectAll());

            popupMenu.show(this,e.getX()+3, e.getY()+3);
        }
    }

    private boolean isAbleToSelectAll(){
        return !("".equalsIgnoreCase(this.getText())||(null==this.getText()));
    }

    private boolean isAbleToCopyAndCut(){

        return (this.getSelectionStart()!=this.getSelectionEnd());
    }

    private boolean isAbleToPaste(){

        Transferable content = this.getToolkit().getSystemClipboard().getContents(this);
        try {
            return (content.getTransferData(DataFlavor.stringFlavor) instanceof String);
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

}
